﻿using System;
using CommunityServer.WebServices.Membership;
using System.Collections.Generic;

namespace CommunityServer.Membership
{
    public class APIUser
    {
        //private Member oMemberservice = oService.AddUser();
        public void AddUser(User oUser)
        {
            //string oGetRole
            MembershipService oMembershipService = Services.Membership.GetGroupService();
            oMembershipService.AddUser(oUser);
        }

        public int GetUserId(string strUserName)
        {
            MembershipService oService = Services.Membership.GetGroupService();

            UserQuery oUserQuery = new UserQuery();
            oUserQuery.Usernames.Add(strUserName);
            var oUser = oService.GetUsers(oUserQuery);

            foreach (var user in oUser)
            {
                //I only expect 1 user
                return user.Id;
            }
            return 0;
        }
        public User GetUser(string strUserName)
        {
            MembershipService oService = Services.Membership.GetGroupService();

            UserQuery oUserQuery = new UserQuery();
            oUserQuery.Usernames.Add(strUserName);
            var oUser = oService.GetUsers(oUserQuery);
            foreach (var user in oUser)
            {
                //I only expect 1 user
                return user;
            }
            User uEmpty = new User();
            return uEmpty;
        }
        /// <summary>
        /// BROEKN: Add by Username Seems to fail; Use Id
        /// </summary>
        /// <param name="pUsername"></param>
        /// <param name="pRole"></param>
        /*
        public void AddUsertoRole(string pUsername, Roles pRole)
        {
            MembershipService oMembershipService = Services.Membership.GetGroupService();
            
            Role oRole = new Role();
            oRole = oMembershipService.GetRole(Convert.ToString(pRole));
            //var oRoles = oMembershipService.GetRoles(pUsername);

            oMembershipService.AddUserToRole(pUsername, oRole);            
        }
         */
        public void AddUsertoRole(int pUserId, Roles pRole)
        {

            APIUser oApiUser = new APIUser();
            List<Role> oRoles;
            oRoles = oApiUser.GetUserRoles(pUserId);
            if (oApiUser.IsMemberInRole(oRoles, Convert.ToString(pRole)))
                return;

            MembershipService oMembershipService = Services.Membership.GetGroupService();

            Role oRole = new Role();
            oRole = oMembershipService.GetRole(Convert.ToString(pRole));
            oMembershipService.AddUserToRole(pUserId, oRole);
        }
        public List<Role> GetUserRoles(int pUserId)
        {
            MembershipService oMembershipService = Services.Membership.GetGroupService();
            return oMembershipService.GetRoles(pUserId);
        }
        public Boolean IsMemberInRole(List<Role> pRoles, string pRole)
        {
            foreach (var role in pRoles)
            {
                if (role.Name == pRole)
                    return true;
            }
            return false;
        }
        public List<User> AllUsers()
        {
            MembershipService oMembershipService = Services.Membership.GetGroupService();
            return oMembershipService.GetUsers();
        }
   }
}